package com.ming.note.action;

import android.app.Application;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.ming.note.R;
import com.ming.note.helper.ActivityStackManager;
import com.ming.note.widget.HintLayout;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/12/08
 * desc   : 界面状态提示
 */
public interface StatusAction {

    /**
     * 获取提示布局
     */
    HintLayout getHintLayout();

    /**
     * 显示加载中
     */
    default void showLoading() {
        showLoading(R.raw.loading);
    }

    default void showLoading(@RawRes int id) {
        HintLayout layout = getHintLayout();
        layout.show();
        //设置动画id
        layout.setAnim(id);
        //隐藏提醒
        layout.setHint("");
        //隐藏按钮
        layout.setBtnText("");
        layout.setOnClickListener(null);
    }

    /**
     * 显示加载完成
     */
    default void showComplete() {
        HintLayout layout = getHintLayout();
        if (layout != null && layout.isShow()) {
            layout.hide();
        }
    }

    /**
     * 显示空提示
     */
    default void showEmpty() {
        showLayout(R.drawable.ic_hint_empty, R.string.hint_layout_no_data, R.string.empty, null);
    }

    default void showEmpty(@DrawableRes int drawableId
            , @StringRes int notifyTxtId
            , @StringRes int btnTxtId
            , View.OnClickListener listener) {

        showLayout(drawableId, notifyTxtId, btnTxtId, listener);
    }

    /**
     * 显示错误提示
     */
    default void showError(View.OnClickListener listener) {
        //网络连接判断
        Application application = ActivityStackManager.getInstance().getApplication();
        if (application != null) {
            ConnectivityManager manager = ContextCompat.getSystemService(application, ConnectivityManager.class);
            if (manager != null) {
                NetworkInfo info = manager.getActiveNetworkInfo();
                // 判断网络是否连接
                if (info == null || !info.isConnected()) {
                    showLayout(R.drawable.ic_hint_network, R.string.hint_layout_error_network, R.string.hint_layout_btn_refresh, listener);
                    return;
                }
            }
        }
        showLayout(R.drawable.ic_hint_error, R.string.hint_layout_error_request, R.string.hint_layout_btn_refresh, listener);
        //只有在错误的时候 弹出头部错误提醒
        getHintLayout().showErrorTxtBanner();
    }

    /**
     * 显示自定义提示
     */
    default void showLayout(@DrawableRes int drawableId
            , @StringRes int notifyTxtId
            , @StringRes int btnTxtId
            , View.OnClickListener listener) {
        if (notifyTxtId == 0){
            notifyTxtId = R.string.hint_layout_no_data;
        }
        showLayout(ContextCompat.getDrawable(ActivityStackManager.getInstance().getApplication(), drawableId)
                , ActivityStackManager.getInstance().getApplication().getString(notifyTxtId)
                , ActivityStackManager.getInstance().getApplication().getString(btnTxtId)
                , listener);
    }

    default void showLayout(Drawable drawable, CharSequence hint, CharSequence btnTxt, View.OnClickListener listener) {
        HintLayout layout = getHintLayout();
        layout.show();
        layout.setIcon(drawable);
        layout.setHint(hint);
        layout.setBtnText(btnTxt);
        layout.setOnClickListener(listener);
    }
}